home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1997 September
/
Macworld (1997-09).dmg
/
Shareware World
/
Utilities
/
Text Processing
/
AlphaLite.6.52
/
Tcl
/
Modes
/
latexSmart.tcl
< prev
next >
Wrap
Text File
|
1996-08-15
|
6KB
|
211 lines
#############################################################################
#############################################################################
#
# latexSmart.tcl (called from latex.tcl)
#
# Smart quotes, dots, subscripts, and superscripts
#
#############################################################################
#
# Original author unknown
#
# Maintainer: Tom Scavo <trscavo@syr.edu>
#
#############################################################################
#############################################################################
#--------------------------------------------------------------------------
# Smart quotes:
#--------------------------------------------------------------------------
proc smartDQuote {} {
global TeXmodeVars
if {[isSelection]} { deleteSelection }
if { !$TeXmodeVars(smartQuotes) || [literalChar] } { insertText {"}; return }
if {[leftQ]} {
insertText {``}
} else {
insertText {''}
}
}
# bind double quote:
ascii 0x22 <s> smartDQuote TeX
proc smartQuote {} {
global TeXmodeVars
if {[isSelection]} { deleteSelection }
if { !$TeXmodeVars(smartQuotes) || [literalChar] } { insertText {'}; return }
if {[leftQ]} {
insertText {`}
} else {
insertText {'}
}
}
# bind single quote:
ascii 0x27 smartQuote TeX
proc leftQ {} {
if { [getPos] == 0 } { return 1 };
set q [lookAt [expr [getPos]-1]]
case $q in {
{\t} {return 1}
{(} {return 1}
{\{} {return 1}
{[} {return 1}
{<} {return 1}
{\ } {return 1}
{\r} {return 1}
}
return 0
}
#--------------------------------------------------------------------------
# Smart dots:
#--------------------------------------------------------------------------
# proc smartDots {} {
# global TeXmodeVars
# if {[isSelection]} { deleteSelection }
# if { !$TeXmodeVars(smartDots) || [literalChar] } { insertText {.}; return }
# # Labels contain literal dots:
# set pat {\\(label|(page|eq)?ref|bibitem|(no)?cite)(\[[^][]*\])?\{}
# if { [findPatJustBefore "$pat" "${pat}\[\]\[()`'.,:;?!a-zA-Z0-9/@*-\]*\$"] != "" } {
# insertText "."
# return
# }
# # Filenames contain literal dots:
# set pat {\\(usepackage|input|include(only)?|documentclass|bibliography(style)?|LoadClass|RequirePackage|begin\{filecontents\})(\[[^][]*\])?\{}
# if { [findPatJustBefore "$pat" "${pat}\[.:a-zA-Z_0-9/-\]*\$"] != "" } {
# insertText "."
# return
# }
# if {[lookAt [expr [set endPos [getPos]]-1]] == "."} then {
# if {[lookAt [set begPos [expr $endPos-2]]] == "."} then {
# replaceText $begPos $endPos "\\ldots"
# } else {
# insertText "."
# }
# } else {
# insertText "."
# }
# }
proc smartDots {} {
global TeXmodeVars
if {[isSelection]} { deleteSelection }
if { !$TeXmodeVars(smartDots) || [literalChar] } { insertText {.}; return }
if {[lookAt [expr [set endPos [getPos]]-1]] == "."} then {
if {[lookAt [set begPos [expr $endPos-2]]] == "."} then {
replaceText $begPos $endPos "\\ldots"
} else {
insertText "."
}
} else {
insertText "."
}
}
# bind period:
ascii 0x2e smartDots "TeX"
#--------------------------------------------------------------------------
# Smart subscripts and superscripts:
#--------------------------------------------------------------------------
proc smartSubscripts {} {
smartScripts {_}
}
proc smartSuperscripts {} {
smartScripts {^}
}
proc smartScripts {char} {
global TeXmodeVars
if {[isSelection]} { deleteSelection }
if { !$TeXmodeVars(smartScripts) || [literalChar] } then {
insertText $char
return
}
# Filenames contain literal underscores:
set pat {\\(usepackage|input|include(only)?|documentclass|bibliography(style)?|LoadClass|RequirePackage|begin\{filecontents\})(\[[^][]*\])?\{}
if { [findPatJustBefore "$pat" "${pat}\[.:a-zA-Z0-9/^_-\]*\$"] != "" } {
insertText $char
return
}
if { $TeXmodeVars(smartScripts) } then {
if { $char == {_} } { subscript } { superscript }
}
}
# bind underscore and caret:
ascii 0x5f <s> smartSubscripts "TeX"
ascii 0x5e <s> smartSuperscripts "TeX"
#--------------------------------------------------------------------------
# Escapes and exceptions:
#--------------------------------------------------------------------------
# This proc has been moved to procs.tcl:
# proc literalChar {} {
# return [expr {[lookAt [expr [getPos] - 1]] == "\\"}]
# }
#
proc escapeSmartStuff {} {
if {![isSelection]} then {
set pos [getPos]
# Escape double quotes:
if { $pos > 1 } then {
set pos2 [expr $pos - 2]
if { [getText $pos2 $pos] == "''" } then {
replaceText $pos2 $pos {"}
return
} elseif { [getText $pos2 $pos] == "``" } then {
replaceText $pos2 $pos {"}
return
}
}
# Escape single quote:
if { $pos > 0 } then {
set pos1 [expr $pos - 1]
if { [lookAt $pos1] == "`" } then {
backSpace
insertText {'}
return
}
}
# Escape dots:
if { $pos > 5 } then {
set pos6 [expr $pos - 6]
if { [getText $pos6 $pos] == "\\ldots" } then {
replaceText $pos6 $pos {...}
return
}
}
# Escape underscore:
if { $pos > 1 && [maxPos] > [expr $pos + 1] } then {
set begPos [expr $pos - 2]
set endPos [expr $pos + 2]
if { [getText $begPos $endPos] == "_\{\}•" } then {
replaceText $begPos $endPos {_}
return
}
}
# Escape caret:
if { $pos > 1 && [maxPos] > [expr $pos + 1] } then {
set begPos [expr $pos - 2]
set endPos [expr $pos + 2]
if { [getText $begPos $endPos] == "^\{\}•" } then {
replaceText $begPos $endPos {^}
return
}
}
}
backSpace
}
# bind delete key:
ascii 0x08 escapeSmartStuff "TeX"